<template>
  <div>
    <Header></Header>
    <div class="blog-box">
      <h2>{{ blog.title }}</h2>
      <router-link :to="{ name: 'BlogEdit', params: { blogId: blog.id } }">
        <el-link type="primary" icon="el-icon-edit" v-if="ownBlog">
          编辑内容
        </el-link>
      </router-link>
      <el-divider></el-divider>
      <div v-html="blog.content" class="markdown-body"></div>
    </div>
  </div>
</template>
<script>
import Header from "../components/Header";
import "github-markdown-css";
export default {
  components: { Header },
  name: "BlogDetail",
  data() {
    return {
      blog: {
        id: "id",
        title: "默认",
        description: "摘要",
        content: "内容",
      },
      ownBlog: false,
    };
  },
  created() {
    //blogid可以通过路径的params来获取
    const _this = this;
    const blogId = this.$route.params.blogId;

    this.$axios.get("/blog/" + blogId).then((res) => {
      const blog = res.data.data;
      _this.blog.id = blog.id;
      _this.blog.title = blog.title;
      _this.blog.description = blog.description;
      //markdown渲染工具,这段代码要放在用户Id判断前面
      var MarkdownIt = require("markdown-it");
      var md = new MarkdownIt();
      var result = md.render(blog.content);
      _this.blog.content = result;
      //判断当前是否有用户id，如果没有就是没登录的人访问，不显示编辑按钮
      if (blog.userId == _this.$store.getters.getUser.id) {
        _this.ownBlog = true;
      }
      
    });
  },
};
</script>

<style>
.blog-box {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  width: 60%;
  padding: 50px;
  margin: auto;
}
</style>